package com.bitao.friend.elasticsearch;

import com.bitao.friend.domain.question.es.QuestionES;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

/**
 * Created with IntelliJ IDEA.
 * Description: ES三种查询方法：难度、标题内容、标题内容难度、父类中findAll全部
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2025-06-07  16:25
 */
@Repository
public interface QuestionRepository extends ElasticsearchRepository<QuestionES, Long> {

    Page<QuestionES> findQuestionByDifficulty(Integer difficulty, Pageable pageable);

    @Query("{\"bool\": {\"should\": [{ \"match\": { \"title\": ?0 } }, { \"match\": { \"content\": ?1 } }], \"minimum_should_match\": 1, \"must\": [{\"term\": {\"difficulty\": ?2}}]}}")
    Page<QuestionES> findByTitleOrContentAndDifficulty(String keywordTitle, String keywordContent, Integer difficulty, Pageable pageable);

    @Query("{\"bool\": {\"should\": [{ \"match\": { \"title\": ?0 } }, { \"match\": { \"content\": ?1 } }], \"minimum_should_match\": 1}}")
    Page<QuestionES> findByTitleOrContent(String keywordTitle, String keywordContent, Pageable pageable);

}
